package com.example.shellcollector;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.provider.Settings;

import com.example.shellcollector.ShowApp.ShowActivity;
import com.example.shellcollector.service.AppInstalledCollectService;
import com.example.shellcollector.service.AppUsageCollectService;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.Calendar;
import java.util.List;


public class MainActivity extends AppCompatActivity {



    private final BottomNavigationView.OnNavigationItemSelectedListener onNavigationLister
            = item -> {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        showFragment(HomeFragment.TAG);
                        return true;
                    case R.id.navigation_dashboard:
                        showFragment(DashboardFragment.TAG);
                        return true;
                    case R.id.navigation_settings:
                        showFragment(SettingsFragment.TAG);
                        return true;
                }
                return false;
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent collectAppUsage = new Intent(getApplicationContext(), AppUsageCollectService.class);
        Intent collectInstalledApps = new Intent(getApplicationContext(), AppInstalledCollectService.class);

        BottomNavigationView navigationView = findViewById(R.id.navigation);
        navigationView.setOnNavigationItemSelectedListener(onNavigationLister);

        startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
        startService(collectInstalledApps);
        startService(collectAppUsage);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();

        if (savedInstanceState == null) {
            showFragment(HomeFragment.TAG);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Intent collectAppUsage = new Intent(getApplicationContext(), AppUsageCollectService.class);
        Intent collectInstalledApps = new Intent(getApplicationContext(), AppInstalledCollectService.class);
        stopService(collectAppUsage);
        stopService(collectInstalledApps);

    }

    private void showFragment(@NonNull String tag) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

        if (fragment == null) {
            switch (tag) {
                case DashboardFragment.TAG:
                    List<UsageStats> list = getUsageStats();
                    fragment = new DashboardFragment();
                    break;
                case SettingsFragment.TAG:
                    fragment = new SettingsFragment();
                    break;
                default:
                    fragment = new HomeFragment();
                    break;
            }
        }

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_layout, fragment, tag)
                .commit();
    }

    public void toAppTime(View view) {
        Intent intent = new Intent(this, ShowActivity.class);
        startActivity(intent);
    }

    private List<UsageStats> getUsageStats() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -5);
        long start = calendar.getTimeInMillis();
        long end = System.currentTimeMillis();
        UsageStatsManager usageStatsManager = getSystemService(UsageStatsManager.class);
        return usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, start, end);
    }
}